package com.ruoyi.system.pool;

import com.jcraft.jsch.Session;
import org.apache.commons.pool2.KeyedPooledObjectFactory;
import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;

/**
 * @description: 客戶端池
 * @author: stone
 * @date: Created by 2022/4/10 19:51
 * @version: 1.0.0
 * @pakeage: com.ruoyi.framework.config
 */
public class SshClientPool extends GenericKeyedObjectPool<String, Session> {
    public SshClientPool(KeyedPooledObjectFactory<String, Session> factory) {
        super(factory);
    }

    public SshClientPool(KeyedPooledObjectFactory<String, Session> factory, GenericKeyedObjectPoolConfig<Session> config) {
        super(factory, config);
    }


    /**
     private static Map<String, SSHClient> Ssh_Client_Pool = new ConcurrentHashMap<>(128);

     public static SSHClient build(THost host) throws Exception {
     SSHClient sshClient = Ssh_Client_Pool.get(host.getHostname());
     if (null == sshClient) {
     sshClient  = new SSHClient();
     sshClient.setPassword(host.getPasswd());
     sshClient.login(host.getUsername(),host.getHostname(),host.getPort());
     Ssh_Client_Pool.put(host.getHostname(), sshClient);
     return sshClient;
     }
     return sshClient;
     }

     public static Integer runScript(THost host, String script) {
     try {
     SSHClient sshClient = build(host);
     host.setRemark(sshClient.sendCmd(script));
     return 1;
     } catch (Exception e) {
     e.printStackTrace();
     Ssh_Client_Pool.remove(host.getHostname());
     host.setRemark("执行脚本错误");
     return 0;
     }

     }
     **/
}

